﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMD.DataAccess.Interface.Dao;
using System.Collections.ObjectModel;
using SmartMD.DataModel.Entity;
using System.Data;

namespace SmartMD.DataAccess.Oracle.DaoImpl
{
    public class UserEntityDao : BaseGenericDaoImpl, IBaseGenericDao
    {
        public bool SaveOrUpdateObject(Object entity)
        {
            UserInfo user = (UserInfo)entity;
            StringBuilder sb = new StringBuilder();
            sb.Append(" insert into");
            sb.Append(" DUO_USER (ID, USERNAME, ROLE, ISLOGED, PWD, DEPARTMENT, LASTMODIFY, REGISTERID)");
            sb.Append(" values(");
            sb.Append(" '" + user.Id + "',");
            sb.Append(" '" + user.Name + "',");
            sb.Append(" '" + user.Role+ "',");
            sb.Append(" '" + user.IsLoged + "',");
            sb.Append(" '" + user.Password + "',");
            sb.Append(" '" + user.Department + "',");
            sb.Append(" to_date ('" + user.LastModify.ToString("yyyy-MM-dd HH:mm:ss") + "' , 'YYYY-MM-DD HH24:MI:SS' ), ");
            sb.Append(" '" + user.RegisterId + "'");
            sb.Append(")");
            bool success = this.dbConn.Insert(sb.ToString());
            return success;
        }

        public bool DeleteObject(Object entity)
        {
            UserInfo item = (UserInfo)entity;
            string deleteSql = "delete from DUO_USER where ID = '" + item.Id + "'";
            return this.dbConn.Delete(deleteSql);
        }

        public ObservableCollection<UserInfo> GetAllUserInfos()
        {
            ObservableCollection<UserInfo> userList = new ObservableCollection<UserInfo>();
            UserInfo userItem;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_USER");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    userItem = new UserInfo();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        userItem.setValue(clmName, dt.Rows[j][clmName]);
                    }

                    userList.Add(userItem);
                }
                return userList;
            }
            return null;
        }

        public UserInfo GetUser(string userName)
        {
            UserInfo userItem = new UserInfo();
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_USER");
            sb.Append(" where USERNAME='" + userName + "'");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        userItem.setValue(clmName, dt.Rows[j][clmName]);
                    }
                }
                return userItem;
            }
            return null;
        }
        public void SetUserLoginFlag(string userName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append("update DUO_USER set ISLOGED = 'TRUE' where USERNAME='" + userName  + "'");
            this.dbConn.Update(sb.ToString());
        }

        public void SetUserLogoutFlag(string userName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append("update DUO_USER set ISLOGED = 'FALSE' where USERNAME='" + userName + "'");
            this.dbConn.Update(sb.ToString());
       
        }
    }
}
